using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace mmq
{
    public partial class TestTreeEditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (TreeEditor1.NodesCount < 1)
            //{
            //    DetailsView1.ChangeMode(DetailsViewMode.Insert);
            //}
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
        }

        protected void DetailsView1_ItemCreated(object sender, EventArgs e)
        {
            if (DetailsView1.FooterRow != null && DetailsView1.Rows.Count>0)
            {
                int commandRowIndex = DetailsView1.Rows.Count - 1 ;

                DetailsViewRow commandRow = DetailsView1.Rows[commandRowIndex] ;

                DataControlFieldCell cell = commandRow.Controls[0] as  DataControlFieldCell;

                foreach(Control ctl in cell.Controls)
                {
                    LinkButton link = ctl as LinkButton;

                    if(link!=null)
                    {
                        // Look specifically for the Delete button 

                        if (link.CommandName == "Delete")

                            link.OnClientClick = "return confirm('Are you sure you want to delete this record?');";
                    }
                }
            }
        }

        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
        {
            TreeEditor1.BindData();
            DetailsView1.DataBind();
            lbl_msg.Text = e.Keys["CategoryID"].ToString() + " Deleted";
        }

        protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            if (!TreeEditor1.IsLeaf || TreeEditor1.NodesCount<2)
            {
                e.Cancel = true;
                lbl_msg.Text = "Can not delete";
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            TreeEditor1.BindData();
            lbl_msg.Text = e.Values[0].ToString() + " inserted";
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            if (!ValidateInput(e.Values))
            {
                e.Cancel = true;
                lbl_msg.Text = "Can not insert";
            }
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            TreeEditor1.BindData();
            DetailsView1.DataBind();
            lbl_msg.Text = e.Keys["CategoryID"].ToString() + " updated";
        }

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            if (!ValidateInput(e.NewValues))
            {
                e.Cancel = true;
                lbl_msg.Text = "Can not Update";
            }
            else
            {
                if (e.CommandArgument.ToString() == "AppendChild")
                {
                    SqlConnection sqlconnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AC_DBConnectionString"].ToString());
                    string sqltext = "INSERT INTO [AC_Category] ([parentID], [title], [description], [cn_title], [cn_description]) VALUES (@parentID, @title, @description,@cn_title, @cn_description)";
                    sqlconnection.Open();
                    using (SqlCommand sqlcommand = new SqlCommand(sqltext, sqlconnection))
                    {
                        sqlcommand.CommandType = CommandType.Text;
                        sqlcommand.Parameters.AddWithValue("@parentID",TreeEditor1.SelectedValue);
                        sqlcommand.Parameters.AddWithValue("@title", e.NewValues["title"]);
                        sqlcommand.Parameters.AddWithValue("@description", e.NewValues["description"]);
                        sqlcommand.Parameters.AddWithValue("@cn_title", e.NewValues["cn_title"]);
                        sqlcommand.Parameters.AddWithValue("@cn_description", e.NewValues["cn_description"]);
                        try
                        {
                            sqlcommand.ExecuteNonQuery();
                            TreeEditor1.BindData();
                            DetailsView1.DataBind();
                            lbl_msg.Text = "Child appended";
                        }
                        catch (SqlException sqlexception)
                        {
                            lbl_msg.Text = "Can not append child <br/>" + sqlexception.ToString();
                        }
                        finally
                        {
                            e.Cancel = true;
                            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                        }
                    }
                    sqlconnection.Close();
                }

            }
        }
        private bool ValidateInput(System.Collections.Specialized.IOrderedDictionary dicInput)
        {
            foreach (DictionaryEntry de in dicInput)
            {
                if (de.Value==null)
                    return false;
            }
            return true;
        }
    }
}
